package com.yx.cloud.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yx.cloud.R;
import com.iot.common.utils.LogUtils;

/**
 * 输入名称通用对话框
 */
public class CommonInputTextDialog extends Dialog {
    private CommonInputTextCallback commonInputTextCallback;
    private Context context;
    private String titleText;
    private String hintText;

    private CommonInputTextDialog( Context context, String hintText,String titleText, CommonInputTextCallback commonInputTextCallback) {
        super(context, R.style.dialog);
        this.hintText = hintText;
        this.titleText = titleText;
        this.commonInputTextCallback = commonInputTextCallback;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_area_rename);

        TextView tv_title_text = findViewById(R.id.tv_dialog_custom_area_name);
        EditText et_input_text = findViewById(R.id.et_new_area_name);
        TextView btn_cancel = findViewById(R.id.tv_cancel_rename_area);
        TextView btn_ensure = findViewById(R.id.tv_ensure_rename_area);

        et_input_text.setText(hintText);
        et_input_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                LogUtils.i("s: "+ s + " l: "+s.length() + " start : "+ start+ " count : "+ count + " after : "+ after);

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtils.i("s: "+ s + " l: "+s.length() + " start : "+ start+ " count : "+ count + " before : "+ before);

            }

            @Override
            public void afterTextChanged(Editable s) {
                LogUtils.i("s: "+ s +" l"+s.toString().getBytes().length);
                et_input_text.setSelection(s.length());
                while(s.toString().getBytes().length > 18){
                    et_input_text.setText(s.subSequence(0,s.length()-1));
                    break;
                }

            }
        });
        tv_title_text.setText(titleText);

        setCanceledOnTouchOutside(false);

        btn_ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                String inputText = et_input_text.getText().toString();
                if (commonInputTextCallback != null) {
                    //如果返回false, 不取消对话框
                    if (commonInputTextCallback.onInputText(inputText)) {
                        dismiss();
                    }
                } else {
                    dismiss();
                }

            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public interface CommonInputTextCallback {
        boolean onInputText(String text);
    }


    public static Builder newBuilder() {
        return new Builder();
    }

    public static final class Builder {
        private CommonInputTextCallback commonInputTextCallback;
        private String hintText;
        private String titleText;
        private Context context;

        private Builder() {
        }

        public Builder context(Context context) {
            this.context = context;
            return this;
        }

        public Builder callback(CommonInputTextCallback commonInputTextCallback) {
            this.commonInputTextCallback = commonInputTextCallback;
            return this;
        }

        public Builder title(String titleText) {
            this.titleText = titleText;
            return this;
        }

        public Builder hint(String hintText) {
            this.hintText = hintText;
            return this;
        }

        public CommonInputTextDialog build() {
            if (context == null) {
                throw new IllegalArgumentException("context should not be null");
            }
            CommonInputTextDialog commonInputTextDialog = new CommonInputTextDialog(context, hintText,titleText, commonInputTextCallback);
            return commonInputTextDialog;
        }
    }
}
